// 判断 tr 数量 来控制 结算div的隐显
function cd(){
    let trs = document.querySelectorAll("tr");
    console.log(trs.length);
    if(trs.length > 1){
        document.querySelector(".next").style.display = "block"
        document.querySelector(".on").style.display = "none"
    }else{
        document.querySelector(".on").style.display = "block"
        document.querySelector(".next").style.display = "none"
    }
}
// 判断有无登录
let uid = localStorage.getItem("uid");
if (uid) {
  loadCartlist();
} else {
  alert("请先登录");
  location.href = "login.html";
}
// 封装一个加载列表的函数
async function loadCartlist() {
  let params = { id: uid };
  let res = await axios.get(CartListAPI, { params });
  let cartListArr = res.data.data;
  let html = "";
  cartListArr.forEach((obj) => {
    html += `
        <tr>
            <td>
                <input type="checkbox" class="sel-ipt" onchange="singleSelClcik()">
                <img src="${obj.pimg}" alt="">
                <span>${obj.pname}</span>
            </td>
            <td align="center">
                <span class = "price">${obj.pprice}</span>
            </td>
            <td align="center">
                <input type="button" value="-" onclick="subBtnClick(this,${obj.pid})"/>
                <input type="text" value="${obj.pnum}" class="pnum-ipt" style="width: 60px;"/>
                <input type="button" value="+" onclick="addBtnClick(this,${obj.pid})"/>
            </td>
            <td align="center">
                <button onclick="delClick(${obj.pid},this)">删除</button>
            </td>
        </tr> `;
  });
  let table = document.querySelector("table");
  table.innerHTML += html;
  cd();
};
// 删除购物车商品 功能
async function delClick(pid,obj){
    let params = { uid, pid };
    let res = await axios.get(CartDelAPI, { params });
    obj.parentNode.parentNode.remove();
    cd();
    contAllPrice();
}
// 给 加  按钮增加点击事件
async function addBtnClick(btn,pid){
    btn.previousElementSibling.value++;
    let pnum = btn.previousElementSibling.value;
    let params = { uid, pid , pnum};
    let res = await axios.get(CartUpdateAPI,{ params });
    contAllPrice();
}
// 减
async function subBtnClick(btn,pid){
    if (btn.nextElementSibling.value <= 1) {
        btn.nextElementSibling.value = 1;
    } else {
        btn.nextElementSibling.value--;
    }
    let pnum = btn.nextElementSibling.value;
    let params = { uid, pid , pnum};
    let res = await axios.get(CartUpdateAPI,{ params });
    contAllPrice();
}
// 全选按钮
function allSelClcik(ipt) {
    let selIpts = document.querySelectorAll(".sel-ipt");
    selIpts.forEach((v) => (v.checked = ipt.checked));
    contAllPrice();
}
// 单选按钮
function singleSelClcik(){
    let selIpts = document.querySelectorAll(".sel-ipt");
    // 伪数组转为真数组
    let arr = [...selIpts];
    let isAll = arr.every((ipt) => ipt.checked == true);
    document.querySelector("#all-sel").checked = isAll;
    contAllPrice();
}
// 计算总价
function contAllPrice(){
    let selIpts = document.querySelectorAll(".sel-ipt");
    let sum = 0;
    selIpts.forEach((ipt) => {
        if (ipt.checked == true) {
            let tr = ipt.parentNode.parentNode;
            let p1 = tr.querySelector(".price");
            let pnumIpt = tr.querySelector(".pnum-ipt");
            // 拿到数量和价格，拿到的是字符串 转成数字
            let price = Number(p1.innerHTML);
            let pnum = Number(pnumIpt.value);
            console.log( price,pnum);
            sum += price * pnum;
        }
    });
    document.querySelector("#zj").innerHTML = sum;
}